package com.google.code.rgbgoesbetween.services;


import java.util.Locale;

import com.google.code.rgbgoesbetween.SHA1PassHasher;
import com.google.code.rgbgoesbetween.data.AccountStatus;
import com.google.code.rgbgoesbetween.data.Profile;

public class SessionManager {

	// Singleton items/methods
	private static SessionManager sessionManager;
	private SessionManager() { }
	
	public static SessionManager getSessionManager() {
		if (sessionManager == null)
			sessionManager = new SessionManager();
		return sessionManager;
	}
	
	// SessionManager information
	private Profile profile;
	private PasswordHasher passHasher;
	
	// Public methods
	public AccountStatus createProfile(String username, String password) {
		Profile profile = DataProvider.getDataProvider().
				createProfile(username, passHasher.hashPassword(password));
		if(profile == null) {
			return AccountStatus.MEMBER_DNE;
		}
		return AccountStatus.STANDARD_MEMBER;
	}
	
	
	public AccountStatus login(String username, String password) {
		username = username.toLowerCase(Locale.US);
		String passHash = passHasher.hashPassword(password);
		Profile p = DataProvider.getDataProvider().getProfile(username, passHash);
		if (p == null) {
			return AccountStatus.MEMBER_DNE;
		} else {
			this.profile = p;
			return p.getAccountStatus();
		}
	}
	
	public int logout() {
		return 0;}
	public Profile requestProfile() {
		return profile;}
	public int updateProfile(Profile profile) {
		return 0;}
	
	// Private methods
	private int finalizeSession() {
		return 0;
	}
		
	public void init() {
		this.passHasher = new SHA1PassHasher();
	}

	public void testInit() {
		this.passHasher = new PasswordHasher() {	
			@Override
			public String hashPassword(String password) {
				return password;
			}
		};
	}	
}
